package com.lhd.service;

import com.lhd.NotFoundException;
import com.lhd.po.Blog;
import com.lhd.util.MarkdownUtils;
import com.lhd.util.MyBeanUtils;
import com.lhd.dao.BlogDao;
import com.lhd.vo.BlogQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by liuhandong on 2021/10/27.
 */
@Service
public class BlogServiceImpl implements BlogService {


    @Autowired
    private BlogDao blogDao;

    @Override
    public Blog getBlog(Long id) {
        return blogDao.findOne(id);
    }

    @Transactional
    @Override
    public Blog getAndConvert(Long id) {
        Blog blog = blogDao.findOne(id);
        if (blog == null) {
            throw new NotFoundException("该博客不存在");
        }
        Blog b = new Blog();
        BeanUtils.copyProperties(blog,b);
        String content = b.getContent();
        b.setContent(MarkdownUtils.markdownToHtmlExtensions(content));

        //blogDao.updateViews(id);//@todo
        return b;
    }


    @Override
    public List<Blog> listBlog(Integer page, BlogQuery blog) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("page",page*5);
        if (blog.getTitle().equals("")){
            map.put("title",null);
        }else {
            map.put("title",blog.getTitle());
        }
        map.put("typeId",blog.getTypeId());
        map.put("recommend",blog.isRecommend());
//        private String title;
//        private Long typeId;
//        private boolean recommend;
        return blogDao.findAllByPage(map);
    }

    @Override
    public Long countBlogByQuery(BlogQuery blog) {
        HashMap<String,Object> map = new HashMap<>();
        if (blog.getTitle() == ""){
            map.put("title",null);
        }else {
            map.put("title",blog.getTitle());
        }
        map.put("typeId",blog.getTypeId());
        map.put("recommend",blog.isRecommend());

        Long aLong = blogDao.countBlogByQuery(map);

        return blogDao.countBlogByQuery(map);
    }

    @Override
    public List<Blog> listBlog(Integer page) {
        return blogDao.findAll(page);
    }


    @Override
    public List<Blog> listBlog(Long tagId, Integer page) {
        List<Long> blogIds = blogDao.getBlogIdsByTagId(tagId);
        List<Blog> res = new ArrayList<>();
        Integer count = blogIds.size();
        Integer rem = count % 3;
        Integer pageNum = ((count/3 + (rem == 0 ? 0 : 1))-1);
        if (page < pageNum){
            for (int i = 0; i < 3; i++) {
                res.add(blogDao.findOne(blogIds.get(page*3+i)));
            }
        }else {
            for (int i = page*3; i < count; i++) {
                res.add(blogDao.findOne(blogIds.get(i)));
            }
        }
        return res;
    }

    @Override
    public List<Blog> listBlogByTypeId(Long typeId,Integer page){
        List<Long> blogIds = blogDao.getBlogIdsByTypeId(typeId); //根据typeid获取对应的blogid列表
        List<Blog> res = new ArrayList<>();
        Integer count = blogIds.size();
        if (count > 3*page+2){  //该页3篇博客全部可以获取
            for (int i = 0; i < 3; i++) {
                res.add(blogDao.findOne(blogIds.get(page*3 + i)));
            }
        }else { //该页3篇博客只能获取部分
            for (int i = page*3; i < count; i++) {
                res.add(blogDao.findOne(blogIds.get(i)));
            }
        }
        return res;
    }

    @Override
    public List<Blog> listBlog(String query, Integer page) {
        //return blogDao.findByQuery(query,page);//@todo
        return null;
    }

    @Override
    public List<Blog> listRecommendBlogTop(Integer size) {
        return blogDao.findTop(size);
    }

    @Override
    public List<Blog> listBlogByPage(Integer page) {   //page*3 page*3+1 page*3+2
        Long count = blogDao.count();
//        if (count < )
        List<Blog> res = blogDao.getThree(page*3);
        return res;
    }

    @Override
    public List<Blog> getTopFiveBlog(Integer page) {
        return blogDao.getTopFiveBlog(page*5);
    }

    @Override
    public List<Blog> getBlogByQurey(Boolean r, Long typeId) {
        return blogDao.getBlogByQurey(r,typeId);
    }

    @Override
    public List<Blog> getBlogByRe(Boolean r) {
        return blogDao.getBlogByRe(r);
    }

    @Override
    public Map<String, List<Blog>> archiveBlog() {
        //List<String> years = blogDao.findGroupYear();//@todo
        Map<String, List<Blog>> map = new HashMap<>();
//        for (String year : years) {//@todo
//            map.put(year, blogDao.findByYear(year));
//        }
        return map;
    }

    @Override
    public Long countBlog() {
        return blogDao.count();
    }


    @Override
    public Integer saveBlog(Blog blog) {
        if (blog.getId() == null) {
            blog.setCreateTime(new Date());
            blog.setUpdateTime(new Date());
            blog.setViews(0);
        } else {
            blog.setUpdateTime(new Date());
        }
        return blogDao.save(blog);
    }

    @Transactional
    @Override
    public Integer updateBlog(Long id, Blog blog) {
        Blog b = blogDao.findOne(id);
        if (b == null) {
            throw new NotFoundException("该博客不存在");
        }
        BeanUtils.copyProperties(blog,b, MyBeanUtils.getNullPropertyNames(blog));
        b.setUpdateTime(new Date());
        return blogDao.update(b);
    }

    @Transactional
    @Override
    public Integer deleteBlog(Long id) {
        return blogDao.delete(id);
    }

    @Override
    public Long getMaxBlogId() {
        return blogDao.getMaxBlogId();
    }
}
